var express = require('express');
var router = express.Router();
var returnDataUtil = require('../util/returnDataUtil.js');
var dbHelper = require("../db/db.js");

/* 用户登录接口 */
router.get('/login', function(req, res, next) {
    var returnData;
    var query = req.query;
    if ( !query.phone || query.phone.length != 11) {
        returnData = returnDataUtil.error();
        returnData.errorInfo = '手机号码长度不正确';
        res.send(returnData);
        return false;
    }
    //if (!query.code || query.code != req.session.code) {
    //    returnData = returnDataUtil.error();
    //    returnData.errorInfo = '验证码不正确';
    //    res.send(returnData);
    //    return false;
    //}
    returnData = returnDataUtil.init();
    dbHelper.db.query("select * from user where phone = ?", [query.phone], function (err, row) {
        if( !row.length ) {
            dbHelper.db.query("insert into user (phone) values (?)", [query.phone], function (newRow) {
                returnData.data = query.phone;
                res.send(returnData);
            })
        } else {
            returnData.data = query.phone;
            res.send(returnData);
        }
        return true;
    });
});


module.exports = router;
